package com.hust.bridgecheck.util;

import com.hust.bridgecheck.entity.*;
import com.hust.bridgecheck.service.BridgeSensor.BridgeService;
import com.hust.bridgecheck.service.BridgeSensor.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Component
public class CrontabJobUtil {
    //每天固定时间去生成日报
    @Autowired
    private BridgeService bridgeService;
    @Autowired
    private SensorService sensorService;
    @Scheduled(cron = "0 33 21 * * ?")
    public void cronReport(){
        //System.out.println(new Date() +" >>fixedDelay执行....");
        //
        //需要操作的数据:
        //生成系统时间
        long Oneday = 24 * 60 * 60 * 1000;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date current = new Date(119,1,7,21,36,00);//先设置一个时间作为系统当前时间
        Date yesterday = new Date();
        long time = current.getTime();
        long yestertime = time - Oneday;
        yesterday.setTime(yestertime);
//        System.out.println(yesterday);
//        System.out.println(current);
        //对时间进行格式化，传给获取SensorData的方法
        String yesterdayFormat = dateFormat.format(yesterday);
        String currentDayFormat = dateFormat.format(time);
        System.out.println(yesterdayFormat);
        System.out.println(currentDayFormat);
        //获取所有桥的信息
        List<Bridge> allBridge = bridgeService.findAllBridge();
        //遍历取桥
        for(int i = 0; i < allBridge.size(); i++)
        {
            //System.out.println(allBridge.get(i).getDomainID());
            //根据桥ID去获取Senor列表
            List<Sensor> allBridgeSenors = sensorService.getBridgeSenors(allBridge.get(i).getID());
            //遍历传感器列表
            for(int j = 0; j < allBridgeSenors.size(); j++) {
                //通过传感器的pararID来获取正常数据和异常数据
                List<SensorData> allBridgeSensorData = sensorService.getSensorDataByParaId(allBridgeSenors.get(j).getParaId(),yesterdayFormat,currentDayFormat);
                List<WarningData> allBridgeWarningData = sensorService.getWarningDataByParaId(allBridgeSenors.get(j).getParaId());
                //获取关于总数据的一些参数
                long SensorDataSum = allBridgeSensorData.size(); //对应data_total_sum
                long WarningSum = allBridgeWarningData.size();//对应warning_sum
                String WarningID = "";
                String FirstWarningID = "";
                String SecondWarningID = "";
                long FirstWarningSum = 0;
                long SecondWarningSum = 0;
                for(int k = 0; k < allBridgeWarningData.size();k++){
                    //遍历异常数据，统计异常数据的ID以及一级预警的个数和二级预警的个数及对应的ID
                    WarningData warningData = allBridgeWarningData.get(k);
                    FirstWarningSum += warningData.getReachFirst();
                    SecondWarningSum += warningData.getReachSecond();
                    //统计id
                    WarningID += warningData.getID() + ";";
                    if(warningData.getReachFirst() == 1){
                        FirstWarningID = FirstWarningID + warningData.getID();
                        if(k != allBridgeWarningData.size() ){
                            //表示不是最后一个,使用分号进行分割
                            FirstWarningID += ";";
                        }
                    }
                    if(warningData.getReachSecond() == 1){
                        SecondWarningID = SecondWarningID + warningData.getID();
                        if(k != allBridgeWarningData.size() ){
                            //表示不是最后一个,使用分号进行分割
                            SecondWarningID += ";";
                        }
                    }
                }
                WarningID += -1;
                FirstWarningID += -1;
                SecondWarningID += -1;
                //生成DailyReport对象
                DailySensorData dailySensorData = new DailySensorData(allBridgeSenors.get(j).getParaId(),allBridge.get(i).getID(),WarningID,FirstWarningID,SecondWarningID,SensorDataSum,WarningSum,FirstWarningSum,SecondWarningSum,currentDayFormat,allBridge.get(i).getHealth(),allBridgeSenors.get(j).getParaTypeCode(),"",0,"系统报表");//数据地址暂时为空，后期存取hdfs地址,0表示系统报表
                //写入DailyData
                sensorService.addDailySensorData(dailySensorData);
            }
        }
    }
}
